
import "./SignUpLoading.scss"
import qs from 'qs'

import { useMount } from 'ahooks'
import { Button, App } from "antd"
import { useRequest } from 'alova/client'
import { lazy, Suspense, useState } from 'react'
import { useNavigate, useLocation } from 'react-router-dom'

import { Http } from '../../Utils/Api'
import { modelStore } from '../../Config/zustand'
import { AddIndexdDB, IndexedDBClear, GetIndexedDB } from '../../Utils/IndexedDB'

const LoadSvg = lazy(() => import('../../View/Loading/LoadSvg'))

const SignUpLoading = ({ data, Off }) => {
    const { notification } = App.useApp()
    const { updateModel } = modelStore()
    const { search } = useLocation()
    const onNavigate = useNavigate()

    const [loading, setLoading] = useState(false)

    const { send: getToken } = useRequest(params => Http({ url: 'posToken', params }), { immediate: false })
    const { send: getUserInfo } = useRequest(() => Http({ url: 'getUserInfo' }), { immediate: false })
    const { send: getMenuTree } = useRequest(() => Http({ url: 'getProjectMenuOldTree' }), { immediate: false })

    // const { send: getOperator } = useRequest(() => Http({ url: 'getOperator' }), { immediate: false })
    // const { send: getOptions } = useRequest(params => Http({ url: 'getCiaoLiaoTypeInfoV2', params }), { immediate: false })
    // const { send: getRoleInfo } = useRequest(params => Http({ url: 'getRoleInfo', params }), { immediate: false })
    const onAdmin = () => onNavigate('/Admin')
    const onStart = () => onNavigate('/Start')

    useMount(async () => {
        updateModel({})
        const key = await GetIndexedDB('key')
        const project = await GetIndexedDB('project')

        // 路由带参数
        if (search) {
            console.log('处理参数')
            // setLoading(true)
            // const urlParams = new URLSearchParams(search)
            // const queryParams = {}
            // urlParams.forEach((value, key) => queryParams[key] = value)
            // try {
            //     const { token_type, access_token } = await getCode(queryParams)
            //     await AddIndexdDB('token', `${token_type} ${access_token}`)
            //     const { list } = await getOperator()
            //     await AddIndexdDB('userInfo', list)
            //     const [{ uid }] = list
            //     const options = await getOptions({ DataType: 'orgInfo' })
            //     await AddIndexdDB('options', options)
            //     const { list: menu } = await getRoleInfo({ uid })
            //     await AddIndexdDB('open', 'Home')
            //     await AddIndexdDB('openPage', ['Home'])
            //     await AddIndexdDB('menu', [Home, ...menu])
            //     setLoading(true)
            // } catch (err) {
            //     IndexedDBClear()
            //     await AddIndexdDB('key', key)
            //     await AddIndexdDB('project', project)
            //     notification.error({ key: 'error', message: '登录失败!', description: err.data })
            //     Off()
            // }
        } else {
            const { username, password } = data
            const { client_Id, client_Secret, grant_Type } = project
            const paramsUser = qs.stringify({ client_secret: client_Secret, client_id: client_Id, grant_type: grant_Type, username, password })

            try {
                const { token_type, access_token } = await getToken(paramsUser)
                await AddIndexdDB('token', `${token_type} ${access_token}`)
                const user = await getUserInfo()
                await AddIndexdDB('user', user)
                const menu = await getMenuTree()
                await AddIndexdDB('menu', menu)

                // const { list } = await getOperator()
                // await AddIndexdDB('userInfo', list)
                // const [{ uid }] = list
                // const options = await getOptions({ DataType: 'orgInfo' })
                // await AddIndexdDB('options', options)
                // const { list: menu } = await getRoleInfo({ uid })
                // await AddIndexdDB('open', 'Home')
                // await AddIndexdDB('openPage', ['Home'])
                // await AddIndexdDB('menu', [Home, ...menu])



                await AddIndexdDB('openKey', 'a0b4f6b8-ef44-4b43-925f-85a299eb52cb')
                await AddIndexdDB('pageList', [{ key: 'a0b4f6b8-ef44-4b43-925f-85a299eb52cb', label: '首页', url: 'Home', icon: 'shou', closable: false }])
                setLoading(true)

            } catch (err) {
                IndexedDBClear()
                await AddIndexdDB('key', key)
                await AddIndexdDB('project', project)
                notification.error({ key: 'error', message: '登录失败!', description: err.data })
                Off()
            }
        }
    })

    return (
        <Suspense>
            <div className='SignUpLoading'>
                {loading ?
                    <div className="onSign">
                        <Button type="primary" size="large" onClick={onAdmin}>后台</Button>
                        <Button size="large" onClick={onStart}>大屏</Button>
                    </div> :
                    <LoadSvg style={{ '--background': 'transparent', '--stroke': '#212121', height: '100%' }} />
                }
            </div>
        </Suspense>
    );
};

export default SignUpLoading